<template>
  <!-- 摄像头列表组件 -->
  <div class="allcameramain">
    <!-- 摄像头列表 -->
    <div class="cameralistmain">
      <div class="camera-title">摄像头列表</div>
      <div v-if="cameraList[0]" class="camera-zlist">
        <div v-for="(item, index) in cameraList" :key="index" class="camera-item" :class="{
          beselect: cameraId === item.iotCameraId,
        }" @click="getcamermes(item.iotCameraId)">
          <i class="el-icon-video-camera"></i>
          {{ item.cameraName }}
        </div>
      </div>
      <!-- 判断有没有摄像头数据 -->
      <div v-if="!cameraList[0]" class="camera-zlist zlistbtn">
        <div class="camera-item btn">暂无摄像头</div>
      </div>
    </div>
  </div>
</template>
<script>
import VideoSurveillanceApi from "@/api/iot-monitoring/video-surveillance";
import Axios from "axios";
export default {
  props: {
    cameralistparam: {
      type: Object,
      default: () => {
        sessionStorage.organizationNo;
      }
    }
  },
  data () {
    return {
      // 当前摄像头id
      cameraId: "",
      // 当前选中摄像头的详细信息
      cameraInfoList: {},
      // 摄像头列表
      cameraList: [],
      // 网页播放地址
      cameraurl: "",
      startUp: ""
    };
  },
  watch: {
    cameralistparam (newval) {
      this.getcameralist(newval);
    }
  },
  mounted () {
  },
  beforeDestroy () {
  },
  methods: {
    // 获取摄像头列表
    getcameralist (params) {
      this.cameraList = [];
      VideoSurveillanceApi.getcameraList({ fishPondId: params.fishPondId }).then(res => {
        if (res.data) {
          this.cameraList = res.data;
          // 判断有无摄像头列表数据
          if (res.data[0]) {
            this.cameraId = res.data[0].iotCameraId;
            this.getcamermes(this.cameraId);
          } else {
            // 无 传不存在的值
            this.getcamermes(-1);
          }
        }
      });
    },
    // 获取某个摄像头详细信息
    getcamermes (id) {
      this.$emit("nowcameraurl", "transCamera");
      this.cameraId = id;
      if (id !== -1) {
        this.getSame(id);
      }
    },
    // 封装获取摄像详细信息
    async getSame (id) {
      VideoSurveillanceApi.getcameramessage(id).then(res => {
        let data = res.data;
        this.cameraInfoList = data.camera;
        this.setcameraurl();
        // const enc = new TextDecoder('utf-8');
        // let res2 = undefined
        // res.arrayBuffer().then(buffer => {
        //   res2 = JSON.parse(enc.decode(new Uint8Array(buffer))) || {};
        //   let data = res2.data;
        //   this.cameraInfoList = data.camera;
        //   this.setcameraurl();
        // })
      });
      if (!(await this.getcams(id)) && id !== -1) {
        this.$emit("nowcameraurl", "downFlow");
        return;
      }
    },
    async getcams (id) {
      let url = `https://tdiot.cloud/iotApplication/cameraOperation/judgeFlow?cameraId=${id}`;
      const { data: res } = await Axios({
        method: "get",
        url: url
      });
      // console.log(res)
      let data = res.data;
      if (res.code !== 0) {
        if (data.flowUse) {
          // this.$message.success("剩余流量" + data.flowUse);
          console.log("剩余流量" + data.flowUse)
        }
        return data.basis;
      } else {
        return false;
      }
    },
    // 拼接摄像头地址
    setcameraurl () {
      // 判断有无摄像头
      if (this.cameraInfoList) {
        if (this.cameraInfoList.cameraInfo.status === 1) {
          this.cameraurl = "";
          if (this.cameraInfoList) {
            this.cameraurl = `https://open.ys7.com/ezopen/h5/iframe?url=ezopen://open.ys7.com/${this.cameraInfoList["deviceSerial"]}/${this.cameraInfoList["channelNo"]}.hd.live&autoplay=1&accessToken=${this.cameraInfoList["accessToken"]}`;
          }
          this.$emit("nowcameraurl", this.cameraurl);
        } else {
          this.$message.warning("摄像头不在线！请稍后重试");
          this.$emit("nowcameraurl", "notCameraData");
          return;
        }
      } else {
        this.$emit("nowcameraurl", "notCameraList");
      }
    },
    // 开始控制
    async startcontrol (i) {
      let params = {
        cameraId: this.cameraId,
        direction: i,
        speed: 1
      };
      await VideoSurveillanceApi.controlcamera(params);
      setTimeout(() => {
        this.stopcontrol();
      }, 1000);
    },
    // 镜像翻转
    async mirrorcontrol () {
      let params = {
        cameraId: this.cameraId,
        direction: 2,
        speed: 1
      };
      await VideoSurveillanceApi.mirrorcamera(params);
      setTimeout(() => {
        this.stopcontrol();
      }, 500);
    },
    // 停止控制
    stopcontrol () {
      let params = {
        cameraId: this.cameraId
      };
      VideoSurveillanceApi.stopcamera(params).then(res => {
        // console.log(res)
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.allcameramain {
  height: 100%;
  width: 100%;
}

.cameralistmain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-flow: nowrap column;
  align-items: center;
  position: relative;
  justify-content: space-between;

  .camera-title {
    position: absolute;
    top: -6px;
    width: 180px;
    height: 48px;
    line-height: 46px;
    background-repeat: no-repeat;
    background-position: center;
    background-size: 100% 100%;
    background-image: url("~@/assets/commont/top-hexagon.png");
    font-size: 18px;
    color: #1d4c6f;
    z-index: 6;
    margin: 0 auto;
    text-align: center;
  }

  .camera-zlist {
    width: 100%;
    height: 100%;
    z-index: 5;
    margin-top: 18px;
    background-color: #fff;
    box-shadow: 0px 0px 3px 1px rgba(0, 0, 0, 0.16);
    border-radius: 8px 8px 0px 0px;
    overflow-y: auto;
  }

  .camera-item {
    cursor: pointer;
    width: 80%;
    padding: 0 10px;
    font-weight: 700;
    font-size: 16px;
    line-height: 40px;
    margin: 0 auto;
    height: 40px !important;
    color: #2a94fc;
    margin-bottom: 10px;
    border-radius: 5px;
    text-align: center;
    // border: 1px solid #fff;
    overflow: hidden;
    border: 1px solid #2a94fc;
    text-overflow: ellipsis;
    border-radius: 29px;
    white-space: nowrap;
  }

  .camera-item:nth-child(1) {
    margin-top: 25px;
  }

  .camera-item:hover {
    // background-image: linear-gradient(180deg, #0098b7 0%, #76f5d2 100%);
    background-color: #2a94fc;
    color: #fff;
  }

  .beselect {
    background-color: #2a94fc;
    color: #fff;
    // background-image: linear-gradient(180deg, #0098b7 0%, #76f5d2 100%);
  }
}

.cameracontrol {
  width: 100%;
  height: calc(70% - 10px);
  margin-top: 10px;

  .controne {
    width: 100%;
    height: 65%;
    background-image: url("~@/assets/camera/ctrlBg.png");
    background-repeat: no-repeat;
    background-size: 90%;
    background-position: center;
    position: relative;

    .contro-m {
      // border: #46c357 1px solid;
      position: relative;
      width: 25%;
      height: 25%;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 40px;
      cursor: pointer;
    }

    .contro-m:hover {
      font-size: 48px;
    }

    .z-top {
      top: 10%;
      left: 37%;
    }

    .z-left {
      top: 12%;
      left: 11%;
    }

    .z-right {
      top: -13%;
      left: 63%;
    }

    .z-bottom {
      top: -10%;
      left: 37%;
    }

    .z-mirror {
      top: -63%;
      left: 37%;
    }
  }

  .controtwo {
    width: 100%;
    height: 15%;
    display: flex;
    justify-content: space-around;

    .contro-left {
      width: 48%;
      height: 100%;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      // border: #46c357 1px solid;
      background-image: url("~@/assets/camera/transformLeft.png");
      background-repeat: no-repeat;
      background-size: 70%;
      background-position: center;
      padding-right: 30px;
      cursor: pointer;
      font-size: 18px;
    }

    .contro-left:hover {
      font-size: 20px;
    }

    .contro-right {
      width: 48%;
      height: 100%;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      // border: #46c357 1px solid;
      background-image: url("~@/assets/camera/transformRight.png");
      background-repeat: no-repeat;
      background-size: 70%;
      background-position: center;
      padding-left: 30px;
      cursor: pointer;
      font-size: 18px;
    }

    .contro-right:hover {
      font-size: 20px;
    }
  }

  .controthree {
    width: 100%;
    height: 17%;
    display: flex;
    justify-content: space-around;
    align-items: center;

    .greenbut {
      width: 28%;
      height: 60%;
      background-color: rgb(132, 180, 111);
      border-radius: 5px;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: 600;
      font-size: 22px;
      cursor: pointer;
    }

    .greenbut:hover {
      font-size: 24px;
    }
  }
}</style>
